package com.css.openapi.service.impl;


import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.css.common.core.constant.Result;
import com.css.common.core.constant.ReturnCode;
import com.css.common.core.enums.YwlxEnum;
import com.css.common.core.exception.ServiceException;
import com.css.common.core.session.SessionUtils;
import com.css.common.core.utils.BaseUtil;
import com.css.common.core.utils.GyUtils;
import com.css.common.core.utils.yl.YlswUtils;
import com.css.openapi.BO.ZrrSmdjReq;
import com.css.openapi.entity.YhZrrxx;
import com.css.openapi.entity.ZfjkpYhFjxxDO;
import com.css.openapi.feign.ZfjkpPtCommonClient;
import com.css.openapi.service.ApiZrrService;
import com.css.openapi.service.IQyYwlxService;
import com.css.openapi.service.YhZrrxxService;
import com.css.openapi.service.ZfjkpYhFjxxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * @description:自然人api服务实现类
 */
@Slf4j
@Service
public class ApiZrrServiceImpl implements ApiZrrService {
    @Resource
    private YhZrrxxService yhZrrxxService;//自然人信息服务

    @Resource
    private ZfjkpYhFjxxService yhFjxxService;

    @Resource
    private ZfjkpPtCommonClient zfjkpPtCommonClient;


    @Override
    @Transactional
    public Result zrrSmdj(ZrrSmdjReq req) {
        log.info("外系统自然实名登记参数：{}", JSON.toJSONString(req));

        ReturnCode result =  zfjkpPtCommonClient.zrrSmdj(req);
        if(!"0".equals(result.getCode())) throw new ServiceException(result.getMsg());
        //农产品自然人保存附件信息
        List<ZfjkpYhFjxxDO> fjxxDOList = new ArrayList<>();
        String hkbhzyUuid = null;
        String hkbbryUuid = null;
        String cwhscjyzmUuid = null;
        if (!BaseUtil.isNull(req.getCwhscjyzm())) {
            hkbhzyUuid = IdUtil.fastSimpleUUID();
            hkbbryUuid = IdUtil.fastSimpleUUID();
            cwhscjyzmUuid = IdUtil.fastSimpleUUID();
            addFjxx(fjxxDOList, hkbhzyUuid, "户口本户主页", req.getHkbhzy());
            addFjxx(fjxxDOList, hkbbryUuid, "户口本本人页", req.getHkbbry());
            addFjxx(fjxxDOList, cwhscjyzmUuid, "村委会生产经营种植养殖证明，盖章", req.getCwhscjyzm());
            boolean is = yhFjxxService.saveBatch(fjxxDOList);
            log.info("保存自然人农产品附件信息zjhm：{},是否成功：{}", req.getZrrsfzjhm(), is);
        }
        //保存自然人信息
        final YhZrrxx insert = new YhZrrxx()
                .setZrrid(req.getZrrsfzjhm())
                .setXm(req.getZrrxm())
                .setSfzjhm(req.getZrrsfzjhm())
                .setCjr(SessionUtils.getCjr())
                .setCwhscjyzm(cwhscjyzmUuid)
                .setHkbhzy(hkbhzyUuid)
                .setHkbbry(hkbbryUuid)
                .setXgsj(LocalDateTime.now());
        final boolean b = this.yhZrrxxService.saveOrUpdate(insert);
        return b ? Result.ok() : Result.error("自然人实名登记失败");
    }

    private void addFjxx(List<ZfjkpYhFjxxDO> fjxxDOList, String uuid, String fjmc, String fjBase64) {
        if(!Objects.isNull(fjBase64)){
            ZfjkpYhFjxxDO fjxxDO = new ZfjkpYhFjxxDO();
            fjxxDO.setUuid(uuid);
            fjxxDO.setFjmc(fjmc);
            fjxxDO.setTp64(fjBase64);
            fjxxDO.setCjsj(LocalDateTime.now());
            fjxxDO.setScbj("0");
            fjxxDO.setCjr(Long.valueOf("0"));
            fjxxDOList.add(fjxxDO);
        }
    }




}
